package com.blog.controller.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.utils.CommonUtil;
import com.blog.utils.Config;
import com.blog.utils.ConstantUtil;

public class DefaultFilter implements Filter {

	@Override
	public void destroy() {
		System.out.println("destroy");
	}

	@Override
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		// 项目BASE_PATH路径过滤器
		if (null == request.getSession().getAttribute(ConstantUtil.BASE_PATH)) {
			request.getSession().setAttribute(ConstantUtil.BASE_PATH,
					CommonUtil.getBasePath(request));
		}

		roleFilter(request, response);

		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		String useLoginVcode = config.getInitParameter("useLoginVcode");
		String useregisterVcode = config.getInitParameter("useregisterVcode");
		String userAdminLoginVcode = config
				.getInitParameter("userAdminLoginVcode");

		Config.useLoginVcode = useLoginVcode.equals("true") ? true : false;
		Config.useregisterVcode = useregisterVcode.equals("true") ? true
				: false;
		Config.userAdminLoginVcode = userAdminLoginVcode.equals("true") ? true
				: false;
	}

	/**
	 * 权限过滤
	 * 
	 * @param request
	 * @param response
	 */
	private void roleFilter(HttpServletRequest request,
			HttpServletResponse response) {

	}
}
